package Utils;

import java.io.FileReader;
import java.util.Random;
import java.util.Scanner;

import Main.SharedPref;
import Utils.Freebase.FB_ParsingUtils;

public class MiscUtils {
	private static String iniFileName = "settings.ini";

	// initialize current working directory
	private static void initCurrentDir() {
		SharedPref.currentDir = System.getProperty("user.dir");
	}

	// initialize file separator character according to the current OS
	private static void initFileSeperator() {
		String osName = System.getProperty("os.name");
		if (osName.contains("Windows")) {
			SharedPref.fileSeparator = "\\";
		} else if (osName.contains("Linux")) {
			SharedPref.fileSeparator = "/";
		}

	}

	/**
	 * Initialize settings for the application runtime
	 */
	private static void initSettings() {

		initCurrentDir();
		initFileSeperator();

		String SettingsfilePath = SharedPref.currentDir
				+ SharedPref.fileSeparator + iniFileName;
		String line = null;

		try {
			Scanner s = new Scanner(new FileReader(SettingsfilePath));
			while (s.hasNextLine()) {

				// read line
				line = s.nextLine();

				// break settings line into two halves separated by '='
				String[] parts = line.split("=");

				if (parts.length != 2) {
					System.out.println("wrong ini input: [" + line
							+ "]\tskipping line...\n");
					return;
				}

				// handle <setting,value>
				HandleSetting(parts[0].trim(), parts[1].trim());
			}

		} catch (Exception e) {
			System.out.println("unexpected error while reading .ini file at + "
					+ SettingsfilePath + " ( " + e + " )");
		}
	}

	/**
	 * Update SharedPref according to the appropriate <setting,value>
	 */
	private static void HandleSetting(String setting, String value) {

		if (setting.equals("DB_Folder\\TV")) {
			SharedPref.Tv_DB_Folder = value;
		}
		
		if (setting.equals("DB_Folder\\FILM")) {
			SharedPref.Film_DB_Folder = value;
		}
		
		if (setting.equals("DB_Folder\\MEASUREMENT_UNIT")) {
			SharedPref.Measurement_Unit_Folder = value;
		}
 
		
	}

	public static void prepareProgram() {

		// initialize settings for sharedPref
		initSettings();

		// initialize freebase parsing values
		FB_ParsingUtils.init();
	}
	
	
	
	public static int GetRandomNumber(int low,int high){
		 Random randomGenerator = new Random();
		 return randomGenerator.nextInt(high) + low;
	}

}
